
import 'package:ble2318ota_plugin/ota_status.dart';

import 'ble2318ota_plugin_platform_interface.dart';

typedef OtaPluginCallBack(OTAStatus type,int progress);

class Ble2318otaPlugin {

  Future<String?> getPlatformVersion() {
    return Ble2318otaPluginPlatform.instance.getPlatformVersion();
  }

  void startOta(String path) {
    Ble2318otaPluginPlatform.instance.startOta(path);
  }

  void stopScan() {
    Ble2318otaPluginPlatform.instance.stopScan();
  }

  void initBlueTools() {
    Ble2318otaPluginPlatform.instance.initBlueTools();
  }

  Future<List<int>> checkFiles(String binPath,String checkPath) {
    return Ble2318otaPluginPlatform.instance.checkFiles(binPath, checkPath);
  }

  void otaListener(OtaPluginCallBack callBack){
    Ble2318otaPluginPlatform.instance.otaListener((call){
      if(call.method == "sendMessage"){
        String data = call.arguments.toString();
        String d1 = data.split(",")[0];
        // String d2 = data.split(",")[1];
        if(d1 == "1"){
          callBack(OTAStatus.STATUS_SEARING,0);
        }
        if(d1 == "2"){
          callBack(OTAStatus.STATUS_SEARCH_END,0);
        }
        if(d1 == "3"){
          callBack(OTAStatus.STATUS_FIND_DEVICE,0);
        }
        if(d1 == "4"){
          callBack(OTAStatus.STATUS_READLY,0);
        }
        if(d1 == "5"){
          callBack(OTAStatus.STATUS_READLY_ERROR,0);
        }
        if(d1 == "6"){
          callBack(OTAStatus.STATUS_DFU_START,0);
        }
        if(d1 == "7"){
          String d2 = data.split(",")[1];
          callBack(OTAStatus.STATUS_DFU_PROGRESS,int.parse(d2));
        }
        if(d1 == "8"){
          callBack(OTAStatus.STATUS_DFU_FINISH,0);
        }
        if(d1 == "9"){
          callBack(OTAStatus.STATUS_DFU_ERROR,0);
        }
      }
      return callBack(OTAStatus.STATUS_DFU_ERROR,0);
    });
  }
}
